import React from 'react'
import { Form, Input, Button, Checkbox, message } from 'antd';
import { UserOutlined, LockOutlined } from '@ant-design/icons';

import logo from '../../assets/imges/logo.png'
import './login.less'

import memoryUtils from '../../utils/memoryUtils'
import storageUtils from '../../utils/storageUtils'
import { reqLogin } from '../../api';

const Login = (props) => {
    const onFinish = async (values) => {
        console.log('Received values of form: ', values);
        const { username, password } = values
        const result = await reqLogin(username, password)
        console.log("登录返回的数据", result)
        if (result.code === 0) {
            message.success("欢迎", username)
        }
    };





    return (
        <div className="login-wrapper">
            <header className='login-header'>
                <img src={logo} alt="logo" />
                <h1>工单管理系统</h1>
            </header>
            <section className='login-content'>
                <h2>用户登录</h2>
                <Form
                    name="normal_login"
                    className="login-form"
                    initialValues={{
                        remember: true,
                    }}
                    onFinish={onFinish}
                >
                    <Form.Item
                        name="username"
                        rules={[
                            { required: true, message: '请输入用户名', messageVariables: 'admin' },
                            { min: 4, message: '用户名最小为4位' },
                            { max: 12, message: '用户名最大为12位' },
                            { pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能由英文、数字或下划线组成' },
                        ]}
                    >
                        <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="用户名" />
                    </Form.Item>
                    <Form.Item
                        name="password"
                        rules={[
                            { required: true, message: '请输入密码' },
                            { min: 4, message: '密码最小为4为' },
                            { max: 18, message: '密码最大为18位' },
                            { pattern: /^[a-zA-Z0-9@_]+$/, message: '密码只能由英文、数字、下划线或@符组成' },
                        ]}
                    >
                        <Input
                            prefix={<LockOutlined className="site-form-item-icon" />}
                            type="password"
                            placeholder="密码"
                        />
                    </Form.Item>
                    <Form.Item>
                        <Form.Item name="remember" valuePropName="checked" noStyle>
                            <Checkbox>记住我</Checkbox>
                        </Form.Item>
                        <a className="login-form-forgot" href="">
                            忘记密码
                        </a>
                    </Form.Item>
                    <Form.Item>
                        <Button type="primary" htmlType="submit" className="login-form-button">
                            登录
                        </Button>
                    </Form.Item>
                </Form>
            </section>
        </div>
    );
}

export default () => <Login />;